package com.example.myblog.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.myblog.dto.BlogRecommendDTO;
import com.example.myblog.dto.SiteDTO;
import com.example.myblog.dto.UserDTO;
import com.example.myblog.entity.BlogMessage;
import com.example.myblog.entity.Category;
import com.example.myblog.entity.Tag;
import com.example.myblog.exception.BaseException;
import com.example.myblog.service.*;
import com.example.myblog.utils.RedisUtils;
import com.example.myblog.utils.UserThreadLocal;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.example.myblog.constants.RedisConstants.USER_LOGIN_KEY;
import static com.example.myblog.constants.UserConstants.USER_HEAD;

/**
 * @author yx
 */
@Service
public class SiteServiceImpl implements SiteService {
    private UserService userService;
    private MessageService messageService;
    private BlogService blogService;
    private BlogMessageService blogMessageService;

    private CategoryService categoryService;

    private BlogTagService blogTagService;

    private TagService tagService;
    private RedisUtils redisUtils;
    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
    @Autowired
    public void setBlogTagService(BlogTagService blogTagService) {
        this.blogTagService = blogTagService;
    }
    @Autowired
    public void setTagService(TagService tagService) {
        this.tagService = tagService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @Autowired
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }
    @Autowired
    public void setBlogService(BlogService blogService) {
        this.blogService = blogService;
    }
    @Autowired
    public void setBlogMessageService(BlogMessageService blogMessageService) {
        this.blogMessageService = blogMessageService;
    }
    @Autowired
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    @Override
    public SiteDTO site(String token) throws BaseException, JsonProcessingException {
        if (token == null) {
            token = "";
        }
        // 1. 返回用户基本信息
        UserDTO dto = redisUtils.getValueByHashNotThrow(USER_LOGIN_KEY+token, new UserDTO());
        if (dto == null) {
            dto = new UserDTO();
            dto.setUserName("暂未登录");
            dto.setUserEmail("没有记录呢");
            dto.setUserHead(USER_HEAD);
        }

        // 2. 返回分类信息
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        List<Category> categories = categoryService.getBaseMapper().selectList(wrapper);
        // 3. 返回标签信息
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        List<Tag> tags = tagService.getBaseMapper().selectList(tagQueryWrapper);
        // 5. 返回推荐博客
        QueryWrapper<BlogMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blog_state",1).orderByAsc("blog_likes").last("limit 5");
        List<BlogMessage> list = blogMessageService.getBaseMapper().selectList(queryWrapper);
        List<BlogRecommendDTO> blogRecommendDTOS = new ArrayList<>();
        for (BlogMessage blogMessage : list) {
            BlogRecommendDTO recommendDTO = new BlogRecommendDTO();
            BeanUtils.copyProperties(blogMessage,recommendDTO);
            blogRecommendDTOS.add(recommendDTO);
        }
        // 4. 加载进 site 中
        SiteDTO siteDTO = new SiteDTO();
        siteDTO.setUserDTO(dto);
        siteDTO.setCategories(categories);
        siteDTO.setTags(tags);
        siteDTO.setRecommendBlogList(blogRecommendDTOS);
        return siteDTO;
    }
}
